#!/bin/sh
AppName="life modem"
itemtoaddA="/Applications/${AppName}.app"
itemtoadd="${AppName}.app"
#application_check1=`sudo -u $SUDO_USER defaults read com.apple.dock persistent-apps | grep -w _CFURLString | awk 'BEGIN { FS = "\"" } ; { print $4 }' | grep -x "${itemtoadd}"`
#application_check2=`sudo -u $SUDO_USER defaults read com.apple.dock persistent-apps | grep -w _CFURLString | awk 'BEGIN { FS = "\"" } ; { print $4 }' | grep -x "${itemtoadd}/"` 

application_check1=`sudo -u $USER defaults read com.apple.dock persistent-apps | grep -w _CFURLString | awk 'BEGIN { FS = "\"" } ; { print $4 }' | grep  "${itemtoadd}"`
application_check2=`sudo -u $USER defaults read com.apple.dock persistent-apps | grep -w _CFURLString | awk 'BEGIN { FS = "\"" } ; { print $4 }' | grep  "${itemtoadd}/"`
if [ ! -d ~/Library/LaunchAgents/ ]; then
	sudo -u $USER mkdir ~/Library/LaunchAgents
fi

systemUserName=$USER
`"${itemtoaddA}"/autoRunFileCheck "${AppName}" "${systemUserName}"`

#cp "${itemtoaddA}/${AppName}" ~/Library/LaunchAgents/
#new auto run way
dirAppSupport=${itemtoaddA}
dirLaunchAgents="/Users/$USER/Library/LaunchAgents"

AppTrayName="Running"
itemTray="Running"
LaunchAgentsPlistTitle="<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"

itemLaunchAgentsPlist=`defaults read "${itemtoaddA}/Contents/Info" CFBundleIdentifier`

sudo rm -f "${dirLaunchAgents}/${itemLaunchAgentsPlist}.plist"

#echo "\n\ndirLaunchAgents=${dirLaunchAgents}\nitemLaunchAgentsPlist=${itemLaunchAgentsPlist}\nLaunchAgentsPlistTitle=${LaunchAgentsPlistTitle}\ndirAppSupport=${dirAppSupport}\nAppTrayName=${AppTrayName}\n\n\n"

defaults write "${dirLaunchAgents}/${itemLaunchAgentsPlist}" "${LaunchAgentsPlistTitle}<plist version=\"1.0\">\
<dict>\
	<key>Label</key>\
		<string>${itemLaunchAgentsPlist}</string>\
	<key>OnDemand</key>\
		<false/>\
	<key>Program</key>\
		<string>${itemtoaddA}/${AppTrayName}</string>\
	<key>RunAtLoad</key>\
		<true/>\
	<key>WorkingDirectory</key>\
		<string>${itemtoaddA}</string>\
</dict>\
</plist>"


#<key>LimitLoadToSessionType</key>\
#<string>Aqua</string>\
#<key>ProgramArguments</key>\
#<array>\
#	<string>${dirAppSupport}/${AppTrayName}</string>\
#	<string>-launchedbylogin</string>\
#</array>\

#end
sudo chown root:wheel "${dirLaunchAgents}/${itemLaunchAgentsPlist}.plist"
sudo chmod 755 "${dirLaunchAgents}/${itemLaunchAgentsPlist}.plist"

chown root "${itemtoaddA}/Contents/Resources/JRDModemService" 
chmod u+xs "${itemtoaddA}/Contents/Resources/JRDModemService"
chmod 777 "${itemtoaddA}/Running"

rm /System/Library/Extensions.kextcache
kextcache -k /System/Library/Extensions
rm -Rf /System/Library/Caches
touch /System/Library/Extensions

if [ "${application_check1}" != "" ] || [ "${application_check2}" != "" ] ; then
    #echo "WARNING! :  This application is in the dock"
    # This application is in the dock
exit 0

else
    sudo -u $USER defaults write com.apple.dock persistent-apps -array-add "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>${itemtoaddA}</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"   
killall "Dock"

fi


exit 0

